package com.cstz.hb.manage.service.impl;

import com.cstz.enums.UserTypeEnum;
import com.cstz.hb.manage.dao.SysQuestionDao;
import com.cstz.hb.manage.domain.AppUser;
import com.cstz.hb.manage.domain.ReportDto;
import com.cstz.hb.manage.domain.SysDept;
import com.cstz.hb.manage.domain.SysQuestion;
import com.cstz.hb.manage.service.AppUserService;
import com.cstz.hb.manage.service.SysDeptService;
import com.cstz.hb.manage.service.SysQuestionService;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SysQuestionServiceImpl implements SysQuestionService {
	@Autowired
	private SysQuestionDao sysQuestionDao;

	@Autowired
    private AppUserService appUserService;

	@Autowired
    private SysDeptService sysDeptService;

	@Override
	public int save(SysQuestion sysQuestion){
	    sysQuestion.setCreateId(sysQuestion.getUserId());
	    sysQuestion.setModifyId(sysQuestion.getUserId());
	    sysQuestion.setCreateTime(new Date());
	    sysQuestion.setModifyTime(new Date());
        return sysQuestionDao.save(sysQuestion);
	}

    public List<SysQuestion> selectQuestions(Map<String, Object> map){
        AppUser appUser = appUserService.get(Long.parseLong(map.get("userId").toString()));
        if(UserTypeEnum.组织部用户.getIndex() == appUser.getType()){
            map.remove("userId");
        }else if(UserTypeEnum.街道用户.getIndex() == appUser.getType()){
            map.remove("userId");
            List<SysDept> sysDepts = sysDeptService.selectDeptsByParentId(appUser.getDeptId());
            if(CollectionUtils.isNotEmpty(sysDepts)){
                map.put("userDeptIds",sysDepts.stream().map(e->e.getId()+"").collect(Collectors.joining(",")));
            }
        }else if(UserTypeEnum.协助单位用户.getIndex() == appUser.getType()){
            map.remove("userId");
            map.put("deptId",appUser.getDeptId());
        }
        return sysQuestionDao.selectQuestions(map);
    }

    public List<SysQuestion> exportData(Map<String, Object> map){
        AppUser appUser = appUserService.get(Long.parseLong(map.get("userId").toString()));
        if(UserTypeEnum.组织部用户.getIndex() == appUser.getType()){
            map.remove("userId");
        }else if(UserTypeEnum.街道用户.getIndex() == appUser.getType()){
            map.remove("userId");
            List<SysDept> sysDepts = sysDeptService.selectDeptsByParentId(appUser.getDeptId());
            if(CollectionUtils.isNotEmpty(sysDepts)){
                map.put("userDeptIds",sysDepts.stream().map(e->e.getId()+"").collect(Collectors.joining(",")));
            }
        }else if(UserTypeEnum.协助单位用户.getIndex() == appUser.getType()){
            map.remove("userId");
            map.put("deptId",appUser.getDeptId());
        }
        return sysQuestionDao.exportData(map);
    }

    @Override
    public int count(Map<String, Object> map){
        return sysQuestionDao.count(map);
    }

    @Override
    public SysQuestion get(Long id){
        return sysQuestionDao.getQuestDetail(id);
    }

    @Override
    public List<ReportDto> analysis(Map<String,Object> map){
        AppUser appUser = appUserService.get(Long.parseLong(map.get("userId").toString()));
        if(UserTypeEnum.组织部用户.getIndex() == appUser.getType()){
            map.remove("userId");
        }else if(UserTypeEnum.街道用户.getIndex() == appUser.getType()){
            map.remove("userId");
            List<SysDept> sysDepts = sysDeptService.selectDeptsByParentId(appUser.getDeptId());
            if(CollectionUtils.isNotEmpty(sysDepts)){
                map.put("userDeptIds",sysDepts.stream().map(e->e.getId()+"").collect(Collectors.joining(",")));
            }
        }else if(UserTypeEnum.协助单位用户.getIndex() == appUser.getType()){
            map.remove("userId");
            map.put("deptId",appUser.getDeptId());
        }
        List<ReportDto> list = Lists.newArrayList();
        switch (Integer.parseInt(map.get("opType").toString())){
            case 1://按状态
                list = sysQuestionDao.analysisByStatus(map);
                break;
            case 2://按社区
                list = sysQuestionDao.analysisByCommunity(map);
                break;
            case 3://按街道
                list = sysQuestionDao.analysisByStreet(map);
                break;
            case 4://按协助单位
                list = sysQuestionDao.analysisByDept(map);
                break;
            case 5://按类型
                list = sysQuestionDao.analysisByType(map);
                break;
            default:
                break;
        }
        return list;
    }
}
